package code;

import java.io.FileWriter;
import java.io.IOException;
//import java.io.UnsupportedEncodingException;
import java.util.*;

import utils.*;
//import utils.Process;

//import com.sun.jna.Library;
//import com.sun.jna.Native;

public class NlpirTest {

	public static void txtCat(int feature_number) throws Exception{
		String testdirc="C:\\Users\\think\\Desktop\\Reduced\\Test\\sports";
		String path[]=new String[5];
		path[0]="C:\\Users\\think\\Desktop\\Reduced\\Train\\Auto";
		path[1]="C:\\Users\\think\\Desktop\\Reduced\\Train\\Education";
		path[2]="C:\\Users\\think\\Desktop\\Reduced\\Train\\Ent";
		path[3]="C:\\Users\\think\\Desktop\\Reduced\\Train\\Money";
		path[4]="C:\\Users\\think\\Desktop\\Reduced\\Train\\Sports";
		HashMap<String,Float> c0=new HashMap<String,Float>();
		HashMap<String,Float> c1=new HashMap<String,Float>();
		HashMap<String,Float> c2=new HashMap<String,Float>();
		HashMap<String,Float> c3=new HashMap<String,Float>();
		HashMap<String,Float> c4=new HashMap<String,Float>();
		c0=Proc.getCPR(path[0],feature_number);
		c1=Proc.getCPR(path[1],feature_number);
		c2=Proc.getCPR(path[2],feature_number);
		c3=Proc.getCPR(path[3],feature_number);
		c4=Proc.getCPR(path[4],feature_number);
		
		int c_sum=0;   //represent the number of a txt that has been classified correctly 
		int flag=0;
		int cflag=0;
		Float pr_final=(float)0;
		String[] txtarr=Proc.getTxtPath(testdirc);
		Float pr[]=new Float[5];
		for(int i=0;i<5;i++){
			pr[i]=(float)0;
		}
		//output to a txt
		FileWriter fw=new FileWriter("C:\\Users\\think\\Desktop\\result_"+feature_number+".txt");		
		for(int i=0;i<txtarr.length;i++){
			System.out.print(String.format("%-60s", txtarr[i]));
			fw.write(String.format("%-60s", txtarr[i]));
			String[] txt=Nlp.strUnique(Nlp.dropStopWords(Nlp.cutWords(txtarr[i])));
			cflag=0;
			flag=0;
			//flag=0    
			if(cflag==0){
				pr[cflag]=Proc.classify_2(txt, c0);
				String temp=String.format("%12f", pr[cflag]);
				System.out.print(temp);
				fw.write(temp);
				cflag=1;
			}
			//flag=1   
			if(cflag==1){
				pr[cflag]=Proc.classify_2(txt, c1);
				String temp=String.format("%12f", pr[cflag]);
				System.out.print(temp);
				fw.write(temp);
				cflag=2;
			}
			//flag=2   
			if(cflag==2){
				pr[cflag]=Proc.classify_2(txt, c2);
				String temp=String.format("%12f", pr[cflag]);
				System.out.print(temp);
				fw.write(temp);
				cflag=3;
			}
			//flag=3   
			if(cflag==3){
				pr[cflag]=Proc.classify_2(txt, c3);
				String temp=String.format("%12f", pr[cflag]);
				System.out.print(temp);
				fw.write(temp);
				cflag=4;
			}
			//flag=4   
			if(cflag==4){
				pr[cflag]=Proc.classify_2(txt, c4);
				String temp=String.format("%12f", pr[cflag]);
				System.out.print(temp);
				fw.write(temp);
				cflag=10;
				
			}
			
			pr_final=(float)0;		
			for(int j=0;j<pr.length;j++){
				
				if(pr[j]>pr_final){
					pr_final=pr[j];
					flag=j;
				}
			}
			if(flag==4){	//0:Auto  1:Culture  2:Economy  3:Medicine  4:Sports
				c_sum++;
			}
			fw.write(Display.getClassLable(flag));
		}
		fw.flush();
		fw.close();
		System.out.println(c_sum);
	}
	
	public static void getFeature(int feature_number) throws Exception{
		String testdirc="E:\\xxxxxxxx\\news\\REDUCED\\auto\\100-200";
		HashMap<String,Float> c0=new HashMap<String,Float>();
		c0=Proc.getCPR(testdirc,feature_number);
		Display.DisFeatureWords(c0);
	}
	
	public static void main(String[] args) {
		int start_num=8;  //
		int end_num=10;    //
		/*for(int i=start_num;i<end_num;i++){
			try {
				txtCat(i);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		try {
			getFeature(10);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	

	
//**********//	
}
